﻿// owner 蒋家伟

#include "DeviceListenAppEventHandler.h"

// jsdk
#include "IApplicationEventArgs.h"
#include "IApplicationEvent.h"
#include "DeviceRegisterHelper.h"
#include "Dbg_Warn_Define.h"
#include "DeviceManager.h"
#include "IDevice.h"
#include "IDeviceEvent.h"
#include "DeviceEventArgs.h"
#include "LogUtils.h"

using namespace jsdk;

#define  SOFTWARE_NAME      L"JSDK"
#define  SOFTWARE_VESION    L"V1.0.0"

namespace
{
    // 全局变量初始化注册到事件，并监听
    DeviceListenAppEventHandler globalListenAppEventHandler;
}

jsdk::DeviceListenAppEventHandler::DeviceListenAppEventHandler()
{
    IApplicationEvent::get()->registerHandler(this);
}

jsdk::DeviceListenAppEventHandler::~DeviceListenAppEventHandler()
{
    IApplicationEvent::get()->unRegisterHandler(this);
}

void jsdk::DeviceListenAppEventHandler::On(const IArgs * pArgs)
{
    if (const IApplicationEventArgs* pApplicationEventArgs = dynamic_cast<const IApplicationEventArgs*>(pArgs))
    {
        if (pApplicationEventArgs->getApplicationStatus() == ApplicationStatus::AfterCreate)
        {
            // 程序启动前设置日志属性
            LogUtils::setSoftwareName(SOFTWARE_NAME);
            LogUtils::setSoftwareVersion(SOFTWARE_VESION);
            DBG_WARN(L"程序启动", L"蒋家伟", L"2023-08-22");
        }
        else if (pApplicationEventArgs->getApplicationStatus() == ApplicationStatus::AfterStart)
        {
            IDeviceEvent& deviceEvent = IDeviceEvent::get();

            // 初始化 前
            DeviceEventArgs deviceEventArgs(DeviceEventType::DeviceBeforeInit);
            deviceEvent.sendEvent(&deviceEventArgs);

            // 注册
            DeviceRegisterHelper* pDeviceRegisterHelper = DeviceRegisterHelper::get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pDeviceRegisterHelper, L"设备助手没有初始化", L"蒋家伟", L"2023-08-04");
            DeviceManager* pDeviceManager = DeviceManager::getFW();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pDeviceManager, L"设备管理器没有初始化", L"蒋家伟", L"2023-08-04");
            for (auto iter : pDeviceRegisterHelper->transOwnerShipCreators())
            {
                OwnerPtr<IDevice> opDevice = iter();
                DBG_WARN_AND_CONTINUE_UNLESS(opDevice, L"设备初始化失败", L"蒋家伟", L"2023-08-04");
                std::wstring deviceName = opDevice->getDeviceName();
                bool success = pDeviceManager->registerDevice(std::move(opDevice));
                DBG_WARN_AND_CONTINUE_UNLESS(success, L"< " + deviceName + L" > 设备重复注册,请检查", L"蒋家伟", L"2023-08-04");
            }

            // 初始化 后
            deviceEventArgs.setEventType(DeviceEventType::DeviceAfterInit);
            deviceEvent.sendEvent(&deviceEventArgs);

            // start 前
            deviceEventArgs.setEventType(DeviceEventType::DeviceBeforeStart);
            deviceEvent.sendEvent(&deviceEventArgs);

            // start
            pDeviceManager->start();

            // start 后
            deviceEventArgs.setEventType(DeviceEventType::DeviceAfterStart);
            deviceEvent.sendEvent(&deviceEventArgs);
        }
        else if (pApplicationEventArgs->getApplicationStatus() == ApplicationStatus::BeforeClose)
        {
            DeviceManager* pDeviceManager = DeviceManager::getFW();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pDeviceManager, L"设备助手没有初始化", L"蒋家伟", L"2023-08-04");
            pDeviceManager->stop();
        }
    }
}
